const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const fsPromise = require("fs/promises");
const compressing = require("compressing");

const log = console.log;
const folder = "/build-resources";
const zipFolder = "/mdesignConfig.zip";
const unpackFile = "mdesignConfig";
const configFile = "customConfig.json";
const themePath = "/files/theme";
const documentPath = "/files/document";
const projectPath = "/files/project";

const configPathTarget = path.join(__dirname, "src", "config"); //  // 将customConfig.json 复制到 src/config下
const themePathTargert = path.join(__dirname, "public", "statics", "theme");
const documentPathTarget = path.join(__dirname, "public", "statics", "files", "document");
const projectPathTarget = path.join(__dirname, "public", "statics", "files", "project");

const analyzeStart = async () => {
  log(chalk.blue("开始解析......"));
  const folderPath = path.join(__dirname, folder);
  if (fs.existsSync(folderPath)) {
    const zipFile = path.join(folderPath, zipFolder);
    if (fs.existsSync(zipFile)) {
      // 解压前先判断build-resources是否已经有mdesignConfig目录，有则先删除
      if (fs.existsSync(path.resolve(folderPath, unpackFile))) {
        fs.rmSync(path.resolve(folderPath, unpackFile), { recursive: true });
      }
      await compressing.zip.uncompress(zipFile, folderPath, {
        zipFileNameEncoding: "GBK"
      });
      analyzeConfig(folderPath);
    } else {
      log(chalk.red("不存在mdesignConfig.zip文件，请检查"));
      process.exit(1);
    }
  } else {
    log(chalk.red("不存在build-resources文件夹，请检查"));
    process.exit(1);
  }
};

const mkDir = (dir) => {
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  } else {
    fs.rmSync(dir, { recursive: true }); //  目录不为空，则先删除该目录，重新复制文件，防止文件内容有更新
    fs.mkdirSync(dir);
  }
};

// 复制目录下的文件到目标目录下
const copyDirs = (sourcePath, targetPath) => {
  const dirs = fs.readdirSync(sourcePath, { encoding: "utf8", withFileTypes: true });
  dirs.forEach((dir) => {
    const originPath = path.resolve(sourcePath, dir.name);
    const copyPath = path.resolve(targetPath, dir.name);
    if (dir.isDirectory()) {
      // 判断是否是文件夹，是的话递归进行
      mkDir(copyPath);
      copyDirs(originPath, copyPath);
    } else {
      fs.copyFileSync(originPath, copyPath);
    }
  });
};

const analyzeConfig = async (configPath) => {
  const sourceFilePath = path.resolve(configPath, unpackFile);
  // log(chalk.blueBright(sourceFilePath)); // E:\workspace\m-design-client\build-resources\mdesignConfig
  const dataJson = await fsPromise.readFile(path.resolve(sourceFilePath, configFile), "utf8");
  const productKey = JSON.parse(dataJson).productKey;
  copyFiles(sourceFilePath, productKey);
  log(chalk.blueBright("文件复制完成"));
  writeToConfig(dataJson);
  log(chalk.blueBright("json文件写入完成"));
};

const copyFiles = (sourceFilePath, dirname) => {
  const fullThemePathTargert = path.resolve(themePathTargert, dirname);
  const fullDocumentPathTarget = path.resolve(documentPathTarget, dirname);
  const fullProjectPathTarget = path.resolve(projectPathTarget, dirname);
  // const fullConfigPathTarget = path.resolve(configPathTarget, dirname);
  // 将customConfig.json 复制到 src/config下
  /*   mkDir(fullConfigPathTarget);
  fs.copyFileSync(path.join(sourceFilePath, configFile), path.join(fullConfigPathTarget, configFile)); */ // 可以不用
  // 将mdesignConfig的theme文件下文件复制到public/statics/theme下，文件夹名称为项目名
  mkDir(fullThemePathTargert);
  copyDirs(path.join(sourceFilePath, themePath, dirname), fullThemePathTargert);
  // 将mdesignConfig的document文件下文件复制到public/statics/files/document下，文件夹名称为项目名
  mkDir(fullDocumentPathTarget);
  copyDirs(path.join(sourceFilePath, documentPath, dirname), fullDocumentPathTarget);
  // 将mdesignConfig的project文件下文件复制到public/statics/files/project下，文件夹名称为项目名
  mkDir(fullProjectPathTarget);
  copyDirs(path.join(sourceFilePath, projectPath, dirname), fullProjectPathTarget);
};

// 将customConfig.json文件写入src/config的customConfig.json
const writeToConfig = (dataJson) => {
  fs.writeFileSync(path.resolve(configPathTarget, configFile), dataJson, "utf8");
};

analyzeStart();
